/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.hazard.om.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.basic.hazard.source.entity.HazHazardSourceEntity;
import com.hopes.bizz.hazard.om.dto.MonitorDataPageDTO;
import com.hopes.bizz.hazard.om.entity.HazRtDataEntity;
import com.hopes.bizz.hazard.om.service.HazRtDataService;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.boss.common.security.annotation.Inner;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 危险源实时监控数据
 *
 * @author daijie
 * @date 2023-11-02 15:18:20
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/rtData")
@Tag(description = "rtData", name = "危险源实时监控数据管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class HazRtDataController {

    private final HazRtDataService hazRtDataService;

    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param hazRtData 危险源实时监控数据
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('haz_rtData_view')")
    public R gethazRtDataPage(Page page, HazRtDataEntity hazRtData) {
        LambdaQueryWrapper<HazRtDataEntity> wrapper = Wrappers.lambdaQuery();
        return R.ok(hazRtDataService.page(page, wrapper));
    }

    /**
     * 传感器状态分页查询
     *
     * @param page      分页对象
     * @param dto 危险源实时监控数据
     * @return
     */
    @Operation(summary = "传感器状态分页查询", description = "传感器状态分页查询")
    @GetMapping("/monitor")
    @PreAuthorize("@pms.hasPermission('haz_rtData_view')")
    public R getMonitorDataPage(Page page, MonitorDataPageDTO dto) {
        return R.ok(hazRtDataService.getMonitorDataPage(page, dto));
    }

    /**
     * 24h数据趋势分析
     *
     * @return
     */
    @Operation(summary = "24h数据趋势分析", description = "24h数据趋势分析")
    @GetMapping("/trendAnalysis")
    @PreAuthorize("@pms.hasPermission('haz_rtData_view')")
    public R trendAnalysis(HazHazardSourceEntity hazHazardSource) {
        return R.ok(hazRtDataService.trendAnalysis(hazHazardSource));
    }

    /**
     * 通过id查询危险源实时监控数据
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('haz_rtData_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(hazRtDataService.getById(id));
    }

    /**
     * 通过监控点因子关系标识 查询危险源实时监控数据
     *
     * @param monitorFactorId id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/getByMonitorFactorId/{monitorFactorId}")
    @Inner
    public R getByMonitorFactorId(@PathVariable("monitorFactorId") Long monitorFactorId) {
        LambdaQueryWrapper<HazRtDataEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(HazRtDataEntity::getMonitorFactorId, monitorFactorId);
        return R.ok(hazRtDataService.getOne(wrapper, false));
    }

    /**
     * 新增危险源实时监控数据
     *
     * @param hazRtData 危险源实时监控数据
     * @return R
     */
    @Operation(summary = "新增危险源实时监控数据", description = "新增危险源实时监控数据")
    @SysLog("新增危险源实时监控数据")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('haz_rtData_add')")
    public R save(@RequestBody HazRtDataEntity hazRtData) {
        return R.ok(hazRtDataService.save(hazRtData));
    }

    /**
     * 修改危险源实时监控数据
     *
     * @param hazRtData 危险源实时监控数据
     * @return R
     */
    @Operation(summary = "修改危险源实时监控数据", description = "修改危险源实时监控数据")
    @SysLog("修改危险源实时监控数据")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('haz_rtData_edit')")
    public R updateById(@RequestBody HazRtDataEntity hazRtData) {
        return R.ok(hazRtDataService.updateById(hazRtData));
    }

    /**
     * 通过id删除危险源实时监控数据
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除危险源实时监控数据", description = "通过id删除危险源实时监控数据")
    @SysLog("通过id删除危险源实时监控数据")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('haz_rtData_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(hazRtDataService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     *
     * @param hazRtData 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('haz_rtData_export')")
    public List<HazRtDataEntity> export(HazRtDataEntity hazRtData) {
        return hazRtDataService.list(Wrappers.query(hazRtData));
    }
}